<?php
namespace app\wxapi\controller;

use app\admin\controller\Getspt;
use app\admin\controller\ReturnCode;
use http\Header;
use think\facade\Cache;
use think\Db;
use think\facade\Log;
use app\admin\controller\Getdata;
use think\Request;

class Messagetemplate extends Base
{
    // 获取区域信息
    protected function getarea()
    {
        $areaarr = array(
            '371601' => '市直管',
            '371602' => '滨城区',
            '371604' => '开发区',
            '371605' => '高新区',
            '371606' => '北海新区',
            '371621' => '惠民县',
            '371622' => '阳信县',
            '371623' => '无棣县',
            '371624' => '沾化区',
            '371625' => '博兴县',
            '371626' => '邹平市',
            '371627' => '工业园区',
            '371628' => '无棣县(市政)'
        );
        return $areaarr;
    }

    // 通过网页授权获取用户openid
    public function getcode(Request $request){
        $data=$request->param();
        $secret = config('apiadmin.secret');
        $openid_gzh=config('apiadmin.appid');
        
        $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$openid_gzh."&secret=".$secret."&code=".$data['code']."&grant_type=authorization_code";
        $res = $this->postinfo('',$url);
        $de_json = json_decode(stripslashes($res),TRUE);
        
        $openid_user=$de_json['openid'];
        $res_prj= Db::table('wx_outh_user')->lock(true)->where('openid','=',$openid_user)->order('id', 'asc')->field('prjid')->find();
        
        if($res_prj == null){
            $url_="http://www.digi-wisdom.site:9527/#/shouquan?openid=".$openid_user;
            header("Location: ".$url_);
        }else{
           $url_="http://www.digi-wisdom.site:9527/#/warn_xm?prjid=".$res_prj['prjid'];
            header("Location: ".$url_);
        }
        exit;
    }

    // 用户授权
    public function add_user_shouquan(Request $request){
        $secret = config('apiadmin.bzsmz_Key');
        $token='JSESSIONID='.$secret;
        $data = $request->param();
        
        $res_prj= Db::table('projectenterprise')->lock(true)->where('code','=',$data['code'])->order('id', 'asc')->field('id')->find();
        $data['prjid']=$res_prj['id'];

        // 验证管理人员手机号码逻辑
        $url_xczy="http://www.bzjsgl.cn/jgbm/jgbm/projectAdmin/xczyry";
        $url_gljs="http://www.bzjsgl.cn/jgbm/jgbm/projectAdmin/gljsry";
        $data['worktype'] =='G001'?$url=$url_gljs:$url=$url_xczy;
        $data['worktype'] =='G001'?$worktype='MANAGER':$worktype='EXPERT';
        
        $post['k']=$data['phone'];
        $post['worktype']=$worktype;
        
        $smz = new Getdata();
        $res = $smz->getinfo($post,$url,$token);
        $de_json = json_decode(stripslashes($res),TRUE);

        if($de_json['totalElements'] >0){
            $res =Db::name('wx_outh_user')->lock(true)->insert($data);
            if($res > 0){
                return $this->buildSuccess($res, true, ReturnCode::SUCCESS);
            }else{
                return $this->buildSuccess($res, '0', ReturnCode::SUCCESS);
            }
        }else{
            return $this->buildSuccess($de_json, false, ReturnCode::SUCCESS);
        }
    }

    // 发送模板消息
    public function templateSend($openid,$template_id,$arr,$arr_){
        $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".Cache::get('access_token');
        $template=array(
            'touser'=>$openid,
            'template_id'=>$template_id,
            'topcolor'=>"#7B68EE",
        );

        foreach ($arr as $k=>$v){
            if($k ==0){
                $template['data']['first']=array('value'=>$v,'color'=>"#FF0000");
            }else{
                $template['data']['keyword'.$k]=array('value'=>$v,'color'=>"#FF0000");
            }
        }
        
        if(strlen($arr_['url']) >0) {
            $template['url'] = $arr_['url'];
        }
        $template['data']['remark']=$arr_['remark'];
        
        $json_template=json_encode($template);
        $res = $this->postinfo($json_template,$url);
        $de_json = json_decode(stripslashes($res),TRUE);
        
        if(isset($de_json['errcode']) && $de_json['errcode'] != 0) {
            if ($de_json['errcode'] != 45009) {
                Cache::rm('access_token');
            }
        }
        return $de_json;
    }

    // 其他微信相关方法...
}